from pms.common.views import PostFormula
from project.models import ProjectCodeRepository
from project.utils.repository import update_repository, repository_view
from project.utils.permission import assert_project_permissions


class API(PostFormula):
    def handle(self, req):
        repo_id = req['id']
        mod = req['mod']
        repository = ProjectCodeRepository.objects.get(id=repo_id)
        assert_project_permissions(repository.pid_id, self.user.id, [2])
        return self.success_respond(repository_view(
            repository=update_repository(
                repository=repository,
                view=mod
            )
        ))
